<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace HyperfTest\Common;

use App\Constants\ErrorCode;
use App\Dao\SendCodeLogDao;
use HyperfTest\HttpTestCase;

/**
 * @internal
 * @coversNothing
 */
class ApiTest extends HttpTestCase
{
//    public function testSendCodeSuccess()
//    {
//        $ret = $this->client->post('/sendCode', [
//            'account' => '15625719964',
//        ]);
//        $this->assertContains($ret['code'], [ErrorCode::SUCCESS, ErrorCode::BUSY_ERROE]);
//    }

    public function testSendCodeFail()
    {
        $ret = $this->client->post('/sendCode', [
        ]);
        $this->assertSame($ret['msg'], '手机号码不能为空');
        $ret = $this->client->post('/sendCode', [
            'account' => '15625741212111',
        ]);
        $this->assertSame($ret['msg'], '手机号码不合规');
    }

    public function testUserLoginFail()
    {
        $ret = $this->client->post('/userLogin', [
        ]);
        $this->assertSame($ret['msg'], '手机号码不能为空');

        $ret = $this->client->post('/userLogin', [
            'account' => '12211',
        ]);
        $this->assertSame($ret['msg'], '手机号码不合规');

        $ret = $this->client->post('/userLogin', [
            'account' => '15625719964',
        ]);
        $this->assertSame($ret['msg'], '验证码不能为空');
        $ret = $this->client->post('/userLogin', [
            'account' => '15625719964',
            'code' => '154121',
        ]);
        $this->assertSame($ret['code'], ErrorCode::CODE_ERROE);
    }

    public function testUserLoginSuccess()
    {
        $account = '15625719964';
        $code = rand_code(6);
        container()->get(SendCodeLogDao::class)->addLog($account, $code);
        $ret = $this->client->post('/userLogin', [
            'account' => $account,
            'code' => $code,
        ]);
        $this->assertSame($ret['code'], ErrorCode::SUCCESS);
    }

    /**
     * 函数测试.
     */
    public function testFunction()
    {
        $number = rand(1, 10);
        $ret = rand_code($number);
        $ret = strval($ret);
        $this->assertEquals(strlen($ret), $number);

        $ret = get_gender_name(1);
        $this->assertEquals($ret, '男');
        $ret = get_gender_name(2);
        $this->assertEquals($ret, '女');
        $ret = get_gender_name(3);
        $this->assertEquals($ret, '未知');
    }
}
